let baseUrl = 'http://ytb.113w.top/api/';

const gHttpBase = () => {
	let url;
	//#ifdef MP || APP
	url = baseUrl + '/'
	//#endif
	//#ifdef H5
	if (process.env.NODE_ENV === 'development') { //开发
		url = '/api/'
		// url = baseUrl + '/api/'
	} else {
		url = baseUrl
	}
	//#endif
	return url

	//#ifdef MP || APP
	// url = baseUrl + '/api'
	url = baseUrl
	//#endif

	return url;
}

const delUndefined = (ob) => {
	if (!ob) {
		return {}
	}
	for (let e in ob) {
		if (typeof(ob[e]) === 'undefined' || ob[e] === null) {
			delete ob[e];
		}
	}
	return ob;
}
let loginBtn = true;
const toLogin = () => {
	if (loginBtn) {
		loginBtn = false;
		setTimeout(() => {
			loginBtn = true;
		}, 3000);
		uni.navigateTo({
			url: '/pages/login/login'
		});
	}
}
let cancelHttp = {};
const postData = (url, data, warning = true) => {
	baseUrl = gHttpBase();
	data = delUndefined(data);
	return new Promise((resolve, reject) => {
		let name = url + JSON.stringify(data);
		let options = {
			url: baseUrl + url,
			method: 'POST',
			header: {
				'uid': uni.getStorageSync('token') || '',
				// 'Content-type': 'application/x-www-form-urlencoded',
				'Content-type': 'application/json',
			},
			data,
			success: res => {
				console.log("success: ", res);
				delete cancelHttp[name];
				if (res.data.code === 401) {
					toLogin();
					return false;
				}
				if (res.data.code === 200) resolve(res.data)
				if (res.data.code !== 200 && warning && (res.msg || res.data.msg)) {
					console.log('error  res: ', res, res.msg);
					uni.hideLoading()
					setTimeout(() => {
						uni.showToast({
							title: res.msg || res.data.msg,
							icon: 'none',
							duration: 3000
						});
					}, 100)
					reject(res.data);
				} else {
					resolve(res.data);
				}
			},
			fail: (err) => {
				delete cancelHttp[name];
				// if (err.errMsg !== 'request:fail abort' && warning) {
				//   uni.showToast({
				//     title: '当前访问人数过多，请稍后重试',
				//     icon: 'none'
				//   });
				// }
				if (err.errMsg !== 'request:fail abort') {
					reject(err)
				}
			}
		}
		let token = uni.getStorageSync('token') || ''
		if (token) options.header["X-Access-Token"] = token
		// if (token) options.header["uid"] = token
		if (cancelHttp[name]) {
			cancelHttp[name].abort();
		}
		cancelHttp[name] = uni.request(options);
	})
	let baseUrl = gHttpBase();
	data = delUndefined(data);
	return new Promise((resolve, reject) => {
		let name = url + JSON.stringify(data);
		let options = {
			url: baseUrl + url,
			method: 'POST',
			header: {
				'content-type': 'application/json',
				// 'Content-type': 'application/x-www-form-urlencoded', 
			},
			data,
			success: res => {
				console.log('res: ', res);
				delete cancelHttp[name];
				if (res.data.code === 401) {
					toLogin();
					return false;
				}
				if (res.code == 200) resolve(res.data)
				if (res.data.code !== 1 && warning) {
					uni.hideLoading()
					setTimeout(() => {
						uni.showToast({
							title: (res.data.msg || '接口出现异常'),
							icon: 'none',
							duration: 3000
						});
					}, 100)
					resolve(res.data);
				} else {
					resolve(res.data);
				}
			},
			fail: (err) => {
				delete cancelHttp[name];
				if (err.errMsg !== 'request:fail abort' && warning) {
					uni.showToast({
						title: '当前访问人数过多，请稍后重试',
						icon: 'none'
					});
				}
				if (err.errMsg !== 'request:fail abort') {
					reject(err)
				}
			}
		}
		options.header["token"] = uni.getStorageSync('token') || ''
		if (cancelHttp[name]) {
			cancelHttp[name].abort();
		}
		cancelHttp[name] = uni.request(options);
	})
}
const getData = function(url, data, warning = true) {
	let baseUrl = gHttpBase();
	data = delUndefined(data);
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: 'GET',
			header: {
				// application/json;charset=UTF-8
				'Content-type': 'application/x-www-form-urlencoded',
				'uid': uni.getStorageSync('token') || ''
			},
			data,
			success: res => {
				console.log('success res: ', res);
				if (res.data.code === 201101) {
					toLogin();
					return false;
				}
				resolve(res.data)
				if (res.code == 200 && warning) {
					uni.hideLoading();
					setTimeout(() => {
						uni.showToast({
							title: (res.data.msg || '接口出现异常'),
							icon: 'none',
							duration: 2000
						});
					}, 10);
				}
			},
			fail: (err) => {
				if (warning) {
					uni.showToast({
						title: '当前访问人数过多，请稍后重试',
						icon: 'none',
						duration: 2000
					});
				}
				reject(err)
			}
		});
	})
}
export default {
	base: gHttpBase,
	baseUrl: baseUrl,
	post: postData,
	get: getData,
	toLogin,
}